package com.netease.financial.base.web;

import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import com.netease.financial.common.util.NfsLog;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wyf on 10/10/15.
 */
public class WebStorageBridge {

    private static final String TAG = WebStorageInterface.class.getSimpleName();

    private static final String WEB_STORAGE_INTERFACE_NAME = "__WebStorageInterface__";

    private final Map<String, CallBack> mLocalStorageCallBackMap;
    private final Map<String, CallBack> mSessionStorageCallBackMap;

    private final WebView mWebView;

    /**
     * 必须在UI线程中实例化这个类
     *
     * @param webView
     */
    public WebStorageBridge(WebView webView) {
        mWebView = webView;
        mLocalStorageCallBackMap = new HashMap<>();
        mSessionStorageCallBackMap = new HashMap<>();
        mWebView.addJavascriptInterface(new WebStorageInterface(), WEB_STORAGE_INTERFACE_NAME);
    }

    public void addLocalStorageItemCallBackListener(String itemName, CallBack callBack) {
        mLocalStorageCallBackMap.put(itemName, callBack);
        callLocalStorageItem(itemName);
    }

    public void saveLocalStorageItem(String itemName, String itemValue) {
        final String jsString = String.format("javascript:localStorage.setItem('%s', '%s')", itemName, itemValue);
        NfsLog.d(TAG, "saveLocalStorageItem.js = " + jsString);
        mWebView.loadUrl(jsString);
    }

    public void addSessionStorageItemCallBackListener(String itemName, CallBack callBack) {
        mSessionStorageCallBackMap.put(itemName, callBack);
        callSessionStorageItem(itemName);
    }

    public void saveSessionStorageItem(String itemName, String itemValue) {
        final String jsString = String.format("javascript:sessionStorage.setItem('%s', '%s')", itemName, itemValue);
        NfsLog.d(TAG, "saveSessionStorageItem.js = " + jsString);
        mWebView.loadUrl(jsString);
    }

    private void callLocalStorageItem(String itemName) {
        String jsString = String.format("javascript:(" +
                "function() {" +
                "    var itemValue = localStorage.getItem('%s');" +
                "    %s.onLocalStorageItemCallBack('%s', itemValue);" +
                "})()", itemName, WEB_STORAGE_INTERFACE_NAME, itemName);

        mWebView.loadUrl(jsString);
    }

    private void callSessionStorageItem(String itemName) {
        String jsString = String.format("javascript:(" +
                "function() {" +
                "    var itemValue = sessionStorage.getItem('%s');" +
                "    %s.onSessionStorageItemCallBack('%s', itemValue);" +
                "})()", itemName, WEB_STORAGE_INTERFACE_NAME, itemName);

        NfsLog.d(TAG, "callSessionStorageItem.js = " + jsString);
        mWebView.loadUrl(jsString);
    }

    public interface CallBack {
        void onItemCallBack(String itemName, String itemValue);
    }

    private class WebStorageInterface {
        @JavascriptInterface
        public void onLocalStorageItemCallBack(String itemName, String itemValue) {
            NfsLog.d(TAG, "onLocalStorageItemCallBack.itemName:" + itemName + ", itemValue:" + itemValue);
            if (mLocalStorageCallBackMap.containsKey(itemName)) {
                mLocalStorageCallBackMap.get(itemName).onItemCallBack(itemName, itemValue);
            }
        }

        @JavascriptInterface
        public void onSessionStorageItemCallBack(String itemName, String itemValue) {
            NfsLog.d(TAG, "onSessionStorageItemCallBack.itemName:" + itemName + ", itemValue:" + itemValue);
            if (mSessionStorageCallBackMap.containsKey(itemName)) {
                mSessionStorageCallBackMap.get(itemName).onItemCallBack(itemName, itemValue);
            }
        }
    }

}
